<?php
/**
 * Created by PhpStorm.
 * Author: 芸签cms www.yunqiancms.com
 * User: sun
 * Date: 2022/6/24
 * Time: 9:31 AM
 */
namespace app\common\helpers;

class Url
{

    /**
     * 返回后端绝对地址
     * @param $url
     * @param array $params
     * @param string $domain
     * @return string
     */
    public static function absoluteAdmin($url, $params = [], $domain = '')
    {
        return self::absolute(self::admin($url,$params));
    }

    /**
     * 返回绝对地址
     * @param $url
     * @param array $params
     * @param string $domain
     * @return string
     */
    public static function absolute($url, $params = [], $domain = '')
    {
        if(empty($url) || self::isHttp($url)){
            return $url;
        }
        empty($domain) && ($domain = request()->getSchemeAndHttpHost());

        return $domain . $url;
    }

    /**
     * 后端url
     * @param $route
     * @param array $params
     * @return string
     */
    public static function admin($route, $params = [])
    {
        if(self::isHttp($route)){
            return $route;
        }
        $defaultParams = ['route'=>$route];
        $params = array_merge($defaultParams, $params);
        return  config('app.admin_path'). '?'. http_build_query($params);
    }

    /**
     * 判断URL是否以http开头
     * @param $url
     * @return bool
     */
    public static function isHttp($url)
    {
        return (strpos($url,'http://') === 0 || strpos($url,'https://') === 0);
    }
}